#!/bin/bash

dbname=$1
dblogin=$2
dbpassword=$3
rhost1=$4
rlogin1=$5
rpassword1=$6
rhost2=$7
rlogin2=$8
archivepassword=$9

print_usage()
{
   echo "Usage: $0 <dbname> <dblogin> <dbpassword> <tct-huizhou rhost> <tct-huizhou rlogin> <tct-huizhou rpassword> <tte-cd-juarez rhost> <tte-cd-juarez rlogin> <tte-cd-juarez rpassword> <archivepassword>"
}

if [ "x$archivepassword" == "x" ]; then
   echo "[ERROR] All arguments not available."
   print_usage
   exit 1
fi

#`rm -f /tmp/run_all_scripts.log`

lockfile="/tmp/run_all_scripts.lock" 
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Checking lock file $lockfile..."
if [ -f $lockfile ]; then 
    echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Lock file exits! Aborting..." 
    exit
fi

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Creating lock file..."
`touch $lockfile` 

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Processing scripts..."

####### Download production data from LC #########################################################

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Retrieving production data from LC..."

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Retrieving archives from tct-huizhou..."

# locked reference
theday=`date +'%y%m%d'`
#theday="080312"

/data/scripts/retrieve_data_lc.sh tct-huizhou $rhost1 $rlogin1 $rpassword1 $dbname $dblogin $dbpassword $archivepassword $theday > /tmp/retrieve_data_tct-huizhou.log 2>&1
cat /tmp/retrieve_data_tct-huizhou.log >> /var/log/retrieve_data_tct-huizhou.log
#/data/scripts/retrieve_data_lc.sh tct-huizhou $rhost1 $rlogin1 $rpassword1 $dbname $dblogin $dbpassword $archivepassword $theday

# not locked reference
/data/scripts/retrieve_data_tct-huizhou_ns.sh $rhost1 $rlogin1 $rpassword1 $dbname $dblogin $dbpassword $archivepassword $theday > /tmp/retrieve_data_tct-huizhou_ns.log 2>&1
cat /tmp/retrieve_data_tct-huizhou_ns.log >> /var/log/retrieve_data_tct-huizhou_ns.log

#echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Retrieving archives from tte_cd_juarez..."

#`rm -f /tmp/getLatamDate.tmp`

#. /data/home/simgen/.bashrc
#/data/scripts/getLatamDate.sh $rlogin2 $rhost2> /tmp/getLatamDate.tmp

#latamProdDay=`for i in \`cat /tmp/getLatamDate.tmp\`; do echo $i | perl -e 'while (<STDIN>) { if ( $_ =~s/^(\d{2})\/(\d{2})\/\d{2}(\d{2})/$3$1$2/){ print; } }'; done`
#echo "latamProdDay => "$latamProdDay

#/data/scripts/retrieve_data_lc.sh tte-cd-juarez $rhost2 $rlogin2 "N/A" $dbname $dblogin $dbpassword $archivepassword $latamProdDay > /tmp/retrieve_data_tte-cd-juarez.log 2>&1
#/data/scripts/retrieve_data_lc.sh tte-cd-juarez $rhost2 $rlogin2 "N/A" $dbname $dblogin $dbpassword $archivepassword $latamProdDay
#cat /tmp/retrieve_data_tte-cd-juarez.log >> /var/log/retrieve_data_tte-cd-juarez.log


####### import seed ################################################################################

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Importing SEED to DB..."
/data/scripts/import_seed.sh $dbname $dblogin $dbpassword > /tmp/import_seed.log 2>&1
cat /tmp/import_seed.log >> /var/log/import_seed.log
#/data/scripts/import_seed.sh $dbname $dblogin $dbpassword

####### import OPL #################################################################################

# center
# tct-huizhou = 2, tte-cd-juarez = 7 

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Importing OPL from tct-huizhou to DB..."
/data/scripts/import_opl.sh S 2 $dbname $dblogin $dbpassword /data/samba_share/tct-huizhou/opl >/tmp/import_opl.sh.log 2>&1
cat /tmp/import_opl.sh.log >> /var/log/import_opl.log
#/data/scripts/import_opl.sh 2 $dbname $dblogin $dbpassword /data/samba_share/tct-huizhou/opl

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Importing ns OPL from tct-huizhou to DB..."
/data/scripts/import_opl.sh NS 2 $dbname $dblogin $dbpassword /data/samba_share/tct-huizhou/opl_ns >/tmp/import_opl.sh.log 2>&1
cat /tmp/import_opl.sh.log >> /var/log/import_opl.log

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Importing OPL from huawei to DB..."
/data/scripts/import_opl.sh S 8 $dbname $dblogin $dbpassword /data/samba_share/huawei/opl >/tmp/import_opl.sh.log 2>&1
cat /tmp/import_opl.sh.log >> /var/log/import_opl.log

#echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Importing OPL from tte_cd_juarez to DB..."

echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: End of process."
echo "`date +'%Y/%m/%d %H:%M:%S'` BASH SCRIPT: Deleting lock file..."
`rm $lockfile` 

error=`grep "\[ERROR\]" /tmp/run_all_scripts.log`
res=$?
if [ $res -eq 0 ]; then
    /data/scripts/sendmail.pl -f multisimgen.support@tcl.com -t raffi.semerciyan@jrdcom.com -s "Multisimgen >run_all_scripts.sh< notification" /tmp/run_all_scripts.log
fi
